package com.orange.notificationsummary.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import com.orange.notificationsummary.R;

/**
 * 
 * @author o
 * 
 */
public class NotificationWidgetProvider extends AppWidgetProvider {

	// private static final String TAG = "NotificationWidgetProvider";

	public static final String			APP_WIDGET_ADD_CONTACT_INTENT	= "com.orange.vipcontact.ADD";
	public static final String			APP_WIDGET_ACTIVITY_INTENT		= "com.orange.vipcontact.DETAIL";

	public static final String			ACTIVITY_PACKAGE_NAME			= "com.orange.notificationsummary";
	public static final String			ACTIVITY_CLASS_NAME				= "com.orange.notificationsummary.SummaryActivity";

	public static final String			SERVICE_PACKAGE_NAME			= "com.orange.lswidget";
	public static final String			SERVICE_CLASS_NAME				= "com.orange.notificationsummary.NotificationWidgetService";

	private static final String			APP_WIDGET_PACKAGE_NAME			= "com.orange.notificationsummary";
	private static final String			APP_WIDGET_CLASS_NAME			= "com.orange.notificationsummary.NotificationWidgetProvider";

	private static final ComponentName	mCn								= new ComponentName(
																				APP_WIDGET_PACKAGE_NAME,
																				APP_WIDGET_CLASS_NAME);

	public static final int				APP_WIDGET_FLAG_DEFAULT_UPDATE	= 0;
	public static final int				APP_WIDGET_FLAG_SERVICE_UPDATE	= 1;

	public static final String			APP_WIDGET_UPDATE_INTENT		= "com.orange.notification.UPDATE";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		final int N = appWidgetIds.length;
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];
			RemoteViews views = getRemoteView(context, new Bundle(),
					APP_WIDGET_FLAG_DEFAULT_UPDATE);
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
		sendIntentToService(context, 0);
	}

	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		sendIntentToService(context, 1);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (APP_WIDGET_UPDATE_INTENT.equalsIgnoreCase(action)) {
			AppWidgetManager appManager = AppWidgetManager.getInstance(context);
			Bundle bd = intent.getExtras();
			RemoteViews views = getRemoteView(context, bd,
					APP_WIDGET_FLAG_SERVICE_UPDATE);
			int[] ids = appManager.getAppWidgetIds(mCn);
			appManager.updateAppWidget(ids, views);
		} else {
			super.onReceive(context, intent);
		}
	}

	/**
	 * get remote view
	 * 
	 * @param context
	 * @param bd
	 * @param processFlag
	 * @return
	 */
	private static synchronized RemoteViews getRemoteView(Context context,
			Bundle bd, int processFlag) {
		int count = 0;
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.notification_layout);
		bindOnClickIntent(context, views);
		switch (processFlag) {
		case APP_WIDGET_FLAG_DEFAULT_UPDATE:
			break;
		case APP_WIDGET_FLAG_SERVICE_UPDATE:
			if (bd != null) {
				count = bd.getInt("NotificationCount");
			}
			break;
		default:
		}
		setEventCounterIndicator(views, count);
		return views;
	}

	/**
	 * set counter indicator for events.
	 * 
	 * @param views
	 * @param count
	 */
	private static void setEventCounterIndicator(RemoteViews views, int count) {
		if (count > 0) {
			if (count < 10) {
				views.setViewVisibility(R.id.notification_count_2, View.GONE);
				views.setViewVisibility(R.id.notification_count_3, View.GONE);
				views.setViewVisibility(R.id.notification_count_1, View.VISIBLE);
				views.setTextViewText(R.id.notification_count_1,
						String.valueOf(count));
			} else if (count < 100) {
				views.setViewVisibility(R.id.notification_count_1, View.GONE);
				views.setViewVisibility(R.id.notification_count_3, View.GONE);
				views.setViewVisibility(R.id.notification_count_2, View.VISIBLE);
				views.setTextViewText(R.id.notification_count_2,
						String.valueOf(count));
			} else if (count < 1000) {
				views.setViewVisibility(R.id.notification_count_1, View.GONE);
				views.setViewVisibility(R.id.notification_count_2, View.GONE);
				views.setViewVisibility(R.id.notification_count_3, View.VISIBLE);
				views.setTextViewText(R.id.notification_count_3,
						String.valueOf(count));
			}
		} else {
			views.setViewVisibility(R.id.notification_count_1, View.GONE);
			views.setViewVisibility(R.id.notification_count_2, View.GONE);
			views.setViewVisibility(R.id.notification_count_3, View.GONE);
		}
	}

	/**
	 * bind pendingIntent
	 * 
	 * @param context
	 * @param views
	 */
	private static void bindOnClickIntent(Context context, RemoteViews views) {
		Intent intent;
		final ComponentName activityName = new ComponentName(
				ACTIVITY_PACKAGE_NAME, ACTIVITY_CLASS_NAME);
		Bundle bd = new Bundle();
		intent = new Intent(ACTIVITY_CLASS_NAME);

		intent.setComponent(activityName);
		intent.putExtras(bd);
		intent.addFlags(1);

		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intent, 0);
		views.setOnClickPendingIntent(R.id.appwidget_layout, pendingIntent);
	}

	/**
	 * start service
	 * 
	 * @param context
	 */
	private static void sendIntentToService(Context context, int flag) {
		Intent intent = new Intent();
		intent.setAction(SERVICE_CLASS_NAME);
		intent.setFlags(flag);
		Bundle bd = new Bundle();
		intent.putExtras(bd);
		try {
			context.startService(intent);
		} catch (SecurityException e) {
		}
	}
}
